package com.xjt.wx_admin.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.xjt.wx_admin.annontation.Anonymous;
import com.xjt.wx_admin.constant.WxConstant;
import com.xjt.wx_admin.entity.User;
import com.xjt.wx_admin.entity.dto.LoginParam;
import com.xjt.wx_admin.service.UserService;
import com.xjt.wx_admin.util.QRCodeUtil;
import com.xjt.wx_admin.util.RedisUtil;
import com.xjt.wx_admin.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 登录控制器
 *
 * @author xujt
 * @date 2023/03/20
 */
@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserService userService;

    /**
     * 得到二维码
     *
     * @param uuid uuid
     * @return {@link R}
     */
    @GetMapping("/getQrCode")
    @Anonymous
    public R<String> getQrCode(String uuid){
        String base64Img = QRCodeUtil.createImage("http://wx.xjtool.top/#/wxLogin?uuid=" + uuid);
        redisUtil.setex(WxConstant.LOGIN_UUID+uuid,WxConstant.LoginType.CREATE.getCode(),5*60);
        return R.ok(base64Img);
    }


    /**
     * 扫描码
     *
     * @param uuid uuid
     * @return {@link R}
     */
    @GetMapping("/scanCode")
    @Anonymous
    public R<Boolean> scanCode(@NotNull String uuid){
        redisUtil.setex(WxConstant.LOGIN_UUID+uuid,WxConstant.LoginType.SCAN.getCode(), 60);
        return R.ok(true);
    }


    /**
     * 确认登录
     *
     * @param uuid      uuid
     * @param loginType 登录类型
     * @return {@link R}
     */
    @GetMapping("/conformLogin")
    @Anonymous
    public R<Boolean> conformLogin(@NotNull String uuid,@NotNull Integer loginType){
        if (loginType.equals(1)) {
//            登录
            redisUtil.setex(WxConstant.LOGIN_UUID+uuid,WxConstant.LoginType.CONFORM.getCode(), 60);
        }else {
//            取消登录
            redisUtil.del(WxConstant.LOGIN_UUID+uuid);
        }
        return R.ok(true);
    }


    /**
     * 查询登录状态
     *
     * @param uuid      uuid
     * @return {@link R}
     */
    @GetMapping("/queryLoginStatus")
    @Anonymous
    public R<Integer> queryLoginStatus(@NotNull String uuid){
        Integer status = (Integer) redisUtil.get(WxConstant.LOGIN_UUID+uuid);
        return R.ok(status);
    }


    /**
     * 确认登录
     * @return {@link R}
     */
    @PostMapping()
    @Anonymous
    public R<String> login(HttpServletRequest request,@RequestBody LoginParam loginParam){
        String ip = ServletUtil.getClientIP(request, null);
        loginParam.setIp(ip);
        return userService.login(loginParam);
    }


    /**
     * 退出登录
     * @return {@link R}
     */
    @GetMapping("/logout")
    public R<Boolean> logout(HttpServletRequest request){
        User user = userService.getById(SecurityUtils.getUserId());
        user.setOnline(0);
        log.info("用户"+user.getUserName()+"退出登录");
        redisUtil.del("login:"+user.getUserName() + ":"+ request.getHeader("Authorization"));
        return R.ok(userService.updateById(user));
    }


    @GetMapping("/getUser")
    public R queryLoginStatus(){
        return R.ok(SecurityUtils.getUser());
    }


}
